﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CMSModel;

public partial class Topics_TypeNameAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            int tid = int.Parse(Request["tid"]);
            CMSEntities cms=new CMSEntities();
            if (cms.Topics2Title.Any(x => x.index == "11" && x.Tid == tid))
            {
                txtTitle11.Text = cms.Topics2Title.First(x => x.index == "11" && x.Tid == tid).Title;
            }
            if (cms.Topics2Title.Any(x => x.index == "21" && x.Tid == tid))
            {
                txtTitle21.Text = cms.Topics2Title.First(x => x.index == "21" && x.Tid == tid).Title;
            }
            if (cms.Topics2Title.Any(x => x.index == "22" && x.Tid == tid))
            {
                txtTitle22.Text = cms.Topics2Title.First(x => x.index == "22" && x.Tid == tid).Title;
            }
            if (cms.Topics2Title.Any(x => x.index == "23" && x.Tid == tid))
            {
                txtTitle23.Text = cms.Topics2Title.First(x => x.index == "23" && x.Tid == tid).Title;
            }
            if (cms.Topics2Title.Any(x => x.index == "31" && x.Tid == tid))
            {
                txtTitle31.Text = cms.Topics2Title.First(x => x.index == "31" && x.Tid == tid).Title;
            }
            if (cms.Topics2Title.Any(x => x.index == "32" && x.Tid == tid))
            {
                txtTitle32.Text = cms.Topics2Title.First(x => x.index == "32" && x.Tid == tid).Title;
            }
            if (cms.Topics2Title.Any(x => x.index == "41" && x.Tid == tid))
            {
                txtTitle41.Text = cms.Topics2Title.First(x => x.index == "41" && x.Tid == tid).Title;
            }
            if (cms.Topics2Title.Any(x => x.index == "51" && x.Tid == tid))
            {
                txtTitle51.Text = cms.Topics2Title.First(x => x.index == "51" && x.Tid == tid).Title;
            }
            if (cms.Topics2Title.Any(x => x.index == "61" && x.Tid == tid))
            {
                txtTitle61.Text = cms.Topics2Title.First(x => x.index == "61" && x.Tid == tid).Title;
            }
            if (cms.Topics2Title.Any(x => x.index == "71" && x.Tid == tid))
            {
                txtTitle71.Text = cms.Topics2Title.First(x => x.index == "71" && x.Tid == tid).Title;
            }
            if (cms.Topics2Title.Any(x => x.index == "72" && x.Tid == tid))
            {
                txtTitle72.Text = cms.Topics2Title.First(x => x.index == "72" && x.Tid == tid).Title;
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        addTitle("11");
        addTitle("21");
        addTitle("22");
        addTitle("23");
        addTitle("31");
        addTitle("32");
        addTitle("41");
        addTitle("51");
        addTitle("61");
        addTitle("71");
        addTitle("72");
        addFirstTitle();
        int tid = int.Parse(Request["tid"]);
        Response.Redirect("Topics_detail.aspx?tid=" + tid.ToString());
    }

    void addFirstTitle()
    {
        int tid = int.Parse(Request["tid"]);
        CMSEntities cms = new CMSEntities();
        Topics2Title t = new Topics2Title();
        bool flagEdit = false;
        if (cms.Topics2Title.Any(x => x.Tid == tid && x.index == "0"))
        {
            t = cms.Topics2Title.First(x => x.Tid == tid && x.index == "0");
            flagEdit = true;
        }
        t.Tid = tid;
        t.Title = "头条";
        t.index = "0";

        if (!flagEdit)
        {
            cms.Topics2Title.AddObject(t);
        }
        cms.SaveChanges();
    }

    void addTitle(string index)
    {
        int tid=int.Parse( Request["tid"]);
        CMSEntities cms = new CMSEntities();
        Topics2Title t = new Topics2Title();
        bool flagEdit = false;
        if (cms.Topics2Title.Any(x => x.Tid == tid && x.index == index))
        {
            t = cms.Topics2Title.First(x => x.Tid == tid && x.index == index);
            flagEdit = true;
        }
        t.Tid = tid;
        t.Title= ((TextBox)(this.FindControl("txtTitle" + index))).Text;
        t.index = index;

        if (!flagEdit)
        {
            cms.Topics2Title.AddObject(t);
        }
        cms.SaveChanges();
    }
}